package com.monitor.controller;


import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.monitor.entity.*;
import com.monitor.service.*;
import com.monitor.util.BlankUtil;
import com.monitor.util.DataUtil;
import com.monitor.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 数据纠错表 前端控制器
 * </p>
 *
 * @author LX
 * @since 2018-09-18
 */
@RestController
@RequestMapping("/erpDataError")
@Api(description="数据纠错")
public class ErpDataErrorController {

    @Resource
    private ErpDataErrorService errorService;
    @Resource
    private ErpCustomerCarService carService;
    @Resource
    private ErpMaterielFlowMaterielService  flowMaterielService;
    @Resource
    private ErpCustomerService customerService;
    @Resource
    private ErpWarehouseService warehouseService;
    @Resource
    private ErpMaterielService materielService;


    @RequestMapping("/getMaterielLikePro")
    @ApiOperation("模糊搜索物料信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "matPro", value = "物料生产编号", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Map<String,Object>> getMaterielLikePro(Integer custId, String matPro) {
        Result<Map<String,Object>>  result=new Result<Map<String,Object>>();
        try{
            Map<String,Object> map=new HashMap<String,Object>();
            List<ErpCustomer> customers=customerService.selectList(new EntityWrapper<ErpCustomer>().eq("cus_service_com",custId));
            ErpCustomer customer=customerService.selectById(custId);
            customers.add(customer);
            List<Integer> custIds=customers.stream().map(ErpCustomer::getId).collect(Collectors.toList());
            String custIdsSTR= BlankUtil.listToString(custIds);
            ErpMateriel materiel=materielService.selectOne(new EntityWrapper<ErpMateriel>().eq("materiel_production_id",matPro));
            boolean b=true;

            for (Integer i:custIds) {
                if(null !=materiel.getWarehouseId()){
                    if (i==materiel.getWarehouseId()) {
                        map.put("materiel", materiel);
                        map.put("customer", customerService.selectById(i));
                        result.setCount(1);
                        result.setData(map);
                    }
                }else {
                    b=false;
                }
            }
            if(b){
                result.setCount(1);
                result.setData(map);
            }

        }catch (Exception e){e.printStackTrace();}
        return result;
    }


    @RequestMapping("/updateMaterielByPro")
    @ApiOperation("修改物料信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "materielID", value = "物料信息ID", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "要修改的物料生产编号", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Integer> updateMaterielByPro(Integer materielID, Integer custId,String code) {
        Result<Integer>  result=new Result<Integer>();
        try{

            //ErpMaterielFlowMateriel flowMateriel=flowMaterielService.selectOne(new EntityWrapper<ErpMaterielFlowMateriel>().eq(""));
            ErpDataError dataError=new ErpDataError();
            ErpCustomer customer=customerService.selectById(custId);
            SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
            String newDate=sdf.format(new Date());
            dataError.setDt(DataUtil.getStringDate());
            ErpMateriel materiel=materielService.selectById(materielID);
            String catOld=materiel.toString();
            materiel.setMaterielProductionId(code);
            String catNew=materiel.toString();
            dataError.setOldData(catOld);
            dataError.setNewData(catNew);
            dataError.setCustomerId(custId);
            dataError.setAttn(customer.getCusName());
            dataError.setCarPalte(warehouseService.selectOne(new EntityWrapper<ErpWarehouse>().eq("customer_id",custId)).getWarehouseId().toString());
            result.setCount(1);
            result.setData(errorService.insertOrUpdate(dataError)?1:0);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }



    @RequestMapping("/updateCat")
    @ApiOperation("修改车牌号换里程表")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Integer> updateCat(ErpCustomerCar customerCar,Integer custId,String newMileage) {//List<ErpCustomerCar>  customerCar,Integer custId
        Result<Integer>  result=new Result<Integer>();
        try{
            ErpDataError dataError=new ErpDataError();
            ErpCustomer customer=customerService.selectById(custId);
            String catOld=customerCar.getCarMileage().toString();
            dataError.setDt(DataUtil.getStringDate());
            dataError.setOldData(catOld);
            dataError.setNewData(newMileage);
            dataError.setCustomerId(custId);
            dataError.setAttn(customerCar.getInputAttn());
            dataError.setCarPalte(customerCar.getCarPlate());
           // dataError.setProblem("修改车辆信息");
            customerCar.setId(customerCar.getId());
            carService.updateById(customerCar);
            result.setCount(1);
            result.setData(errorService.insertOrUpdate(dataError)?1:0);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/getCarLikePro")
    @ApiOperation("模糊搜索车辆数据")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "carPro", value = "车牌号", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")

    })
    public @ResponseBody
    Result<List<ErpCustomerCar>> getCarLikePro(Integer custId,String carPro,String name) {
        Result<List<ErpCustomerCar>>  result=new Result<List<ErpCustomerCar>>();
        try{
            List<ErpCustomer> customers=customerService.selectList(new EntityWrapper<ErpCustomer>().eq("cus_service_com",custId));
            ErpCustomer customer=customerService.selectOne(new EntityWrapper<ErpCustomer>().eq("id",custId).like("cus_name",name));
            customers.add(customer);
            List<Integer> custIds=customers.stream().map(ErpCustomer::getId).collect(Collectors.toList());
            String custIdsSTR= BlankUtil.listToString(custIds);
            List<ErpCustomerCar> cars=new ArrayList<ErpCustomerCar>();
            for (int i = 0; i < cars.size(); i++) {
                ErpCustomerCar erpCustomerCar =  cars.get(i);
                erpCustomerCar.setCustName(customer.getCusName());
                erpCustomerCar.setCusCode(customer.getCusCode());
            }
            if(StringUtils.isEmpty(carPro)){
                cars=carService.selectList(new EntityWrapper<ErpCustomerCar>().in("customer_id",custIdsSTR));
            }else{
                cars=carService.selectList(new EntityWrapper<ErpCustomerCar>().like("car_plate",carPro).or().eq("car_brand",carPro).or().eq("car_model",carPro).or().like("car_number",carPro).and().in("customer_id",custIdsSTR));
            }

            result.setCount(1);
            result.setData(cars);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }




    @RequestMapping("/inertOrUpdateError")
    @ApiOperation("添加Or修改数据纠错")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")*/
    })
    public @ResponseBody
    Result<Integer> inertOrUpdateRepair(ErpDataError ErrorRepair) {
        Result<Integer>  result=new Result<Integer>();
        try{
            result.setCount(1);
            result.setData(errorService.insertOrUpdate(ErrorRepair)?1:0);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/getErrorBycustId")
    @ApiOperation("获取数据纠错")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<List<ErpDataError>> getErrorBycustId(Integer custId) {
        Result<List<ErpDataError>> result=new Result<List<ErpDataError>>();
        try{
            List<ErpDataError> customerCar=errorService.selectList(new EntityWrapper<ErpDataError>().eq("customer_id",custId));
            result.setCount(1);
            result.setData(customerCar);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }


    @RequestMapping("/getErrorByid")
    @ApiOperation("根据id获取数据纠错")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "id", value = "数据纠错id", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<ErpDataError> getErrorRepairByPid(Integer id) {
        Result<ErpDataError> result=new Result<ErpDataError>();
        try{
            ErpDataError customerCar=errorService.selectOne(new EntityWrapper<ErpDataError>().eq("id",id));
            result.setCount(1);
            result.setData(customerCar);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/removeErrorById")
    @ApiOperation("根据id删除数据纠错")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Integer> removeErrorById(Integer id) {
        Result<Integer> result=new Result<Integer>();
        try{
            result.setCount(1);
            result.setData(errorService.deleteById(id)?1:0);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/selectDataErr")
    @ApiOperation("数据纠错车辆里程查询")
    @ApiImplicitParam(name = "keyWord", value = "关键字", required = false, dataType = "String", paramType = "query")
    public Result<List<Map<String, Object>>> selectDataErr(String keyWord){
    	return errorService.selectDataErr(keyWord);
    }

    @RequestMapping("/selectEditTire")
    @ApiOperation("数据纠错改胎信息查询")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "startTime", value = "开始时间", required = false, dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "endTime", value = "结束时间", required = false, dataType = "String", paramType = "query"),
    	@ApiImplicitParam(name = "custId", value = "客户id", required = false, dataType = "int", paramType = "query")
    })
    public Result<List<Map<String, Object>>> selectEditTire(int custId,@RequestParam(value="startTime",defaultValue="")String startTime,@RequestParam(value="endTime",defaultValue="")String endTime){
    	return errorService.selectEditTire(custId, startTime, endTime);
    }
    @RequestMapping("/selectEditTireByNumber")
    @ApiOperation("数据纠错改胎信息查询通过生产编号模糊查询")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "startTime", value = "开始时间", required = false, dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "endTime", value = "结束时间", required = false, dataType = "String", paramType = "query"),
    	@ApiImplicitParam(name = "custId", value = "客户id", required = false, dataType = "int", paramType = "query")
    })
    public Result<List<Map<String, Object>>> selectEditTireByNumber(int custId,@RequestParam(value="startTime",defaultValue="")String startTime,@RequestParam(value="endTime",defaultValue="")String endTime,String number){
        Result<List<Map<String, Object>>> listResult = errorService.selectEditTire(custId, startTime, endTime);
        List<Map<String, Object>> data = listResult.getData();
        List<Map<String, Object>> data2 = new ArrayList<>();
        data.parallelStream().forEach(map->{
            if (String.valueOf(map.get("productionId")).contains(number)){
                data2.add(map);
            }

        });
        listResult.setData(data2);
        return listResult;
    }

}

